using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;

public class TaskManager : LogicModuleBase, ITaskManager
{
    public int TaskNumber = 6;
    public int TaskNumberInMenu = 3;
    public int CanPlayerGetNumber = 1;
    public struct Task
    {
        public string name;
        public string describe;
        public int hardLevel;
        public int money;
        public int targetID;
        public int minCoach;
    }
    public Task[] AllTasks { set; get; }
    public Task[] NextTasks { set; get; }
    public List<Task> TasksOfPlayer { set; get; }

    private MainUI _mainUI;

    public void Instantiate()
    {
        AllTasks = new Task[TaskNumber];
        NextTasks = new Task[TaskNumberInMenu];
        TasksOfPlayer = new List<Task>();
        AllTasks[0].name = "Task0";
        AllTasks[0].describe = "Task0";
        AllTasks[0].hardLevel = 1;
        AllTasks[0].money = 100;
        AllTasks[0].targetID = 0;
        AllTasks[0].minCoach = 2;
        AllTasks[1].name = "Task1";
        AllTasks[1].describe = "Task1";
        AllTasks[1].hardLevel = 1;
        AllTasks[1].money = 100;
        AllTasks[1].targetID = 0;
        AllTasks[1].minCoach = 2;
        AllTasks[2].name = "Task2";
        AllTasks[2].describe = "Task2";
        AllTasks[2].hardLevel = 1;
        AllTasks[2].money = 100;
        AllTasks[2].targetID = 0;
        AllTasks[2].minCoach = 2;
        AllTasks[3].name = "Task3";
        AllTasks[3].describe = "Task3";
        AllTasks[3].hardLevel = 1;
        AllTasks[3].money = 100;
        AllTasks[3].targetID = 0;
        AllTasks[3].minCoach = 2;
        AllTasks[4].name = "Task4";
        AllTasks[4].describe = "Task4";
        AllTasks[4].hardLevel = 1;
        AllTasks[4].money = 100;
        AllTasks[4].targetID = 0;
        AllTasks[4].minCoach = 2;
        AllTasks[5].name = "Task5";
        AllTasks[5].describe = "Task5";
        AllTasks[5].hardLevel = 1;
        AllTasks[5].money = 100;
        AllTasks[5].targetID = 0;
        AllTasks[5].minCoach = 2;
    }
    public override void Init()
    {
        Instantiate();
    }

    public void CreatNextTasks()
    {
        List<Task> allTasks;
        allTasks = new List<Task>();
        for(int i = 0; i < TaskNumber; i++)
        {
            allTasks.Add(AllTasks[i]);
        }
        for (int i = 0; i < TaskNumberInMenu; i++)
        {
            int j = Random.Range(0, allTasks.Count);
            NextTasks[i] = allTasks[j];
            allTasks.Remove(allTasks[j]);
        }
    }
    public void PlayerRemoveTask(int idOfPlayerTask = 0)
    {
        TasksOfPlayer.Remove(TasksOfPlayer[idOfPlayerTask]);
        _mainUI.TaskUIOfPlayer();
    }
    public bool PlayerGetTasks(int idOfTask)
    {
        if (TasksOfPlayer.Count < CanPlayerGetNumber)
        {
            TasksOfPlayer.Add(NextTasks[idOfTask]);
            _mainUI.TaskUIOfPlayer();
            return true;
        }
        else return false;
    }
    public bool IfFinishTask(int currentLocationID, int currentCoach ,int idOfTasksOfPlayer = 0 )
    {
        if (TasksOfPlayer.Count == 0) 
            return false;
        if (currentLocationID == TasksOfPlayer[idOfTasksOfPlayer].targetID
          && currentCoach >= TasksOfPlayer[idOfTasksOfPlayer].minCoach)
        {
            //HandleMonty(TasksOfPlayer[idOfTasksOfPlayer]);
            TasksOfPlayer.Remove(TasksOfPlayer[idOfTasksOfPlayer]);
            return true;
        }
        return false;
    }

    //private void HandleMoney(Task task){Player.Money+=task.money;}

    public override void Update()
    {
        if (_mainUI == null && ProcedureManager.Instance.Current is GameProcedure)
            _mainUI = UIManager.Instance.GetUI("MainUI").GetComponent<MainUI>();
    }
}
